let cp = require('cp');
require('./page.css');
let moduleExport = {};
let vm = avalon.define({
  $id: 'mc_shophome',
  info: {
    updatetime: '',
    shop: '',
    third: '',
    total: '',
    yesterday: '',
  },
  date: {
    stime: '',
    etime: '',
  },
  formatTime: (time, type, sec) => {
    return cp.formatTime(time, type, sec);
  },
  init: () => {
    cp.ajaxUrl({
      controller: 'shopinfo',
      cmd: 'com.rel.porg.pb.shop_home_page_base_req',
      async: false, // 请使用同步
      data: {},
      success: res => {
        if (res.third.wxmpauthid === undefined) {
          res.third.wxmpauthid = 0;
        }
        if (res.third.wxappauthid === undefined) {
          res.third.wxappauthid = 0;
        }
        if (res.third.aliappauthid === undefined) {
          res.third.aliappauthid = 0;
        }
        if (res.third.alilifeauthid === undefined) {
          res.third.alilifeauthid = 0;
        }
        if (res.total.view === undefined) {
          res.total.view = 0;
        }
        if (res.total.visitor === undefined) {
          res.total.visitor = 0;
        }
        if (res.total.ordernum === undefined) {
          res.total.ordernum = 0;
        }
        if (res.total.turnover === undefined) {
          res.total.turnover = 0;
        }

        if (res.yesterday.view === undefined) {
          res.yesterday.view = 0;
        }
        if (res.yesterday.visitor === undefined) {
          res.yesterday.visitor = 0;
        }
        if (res.yesterday.ordernum === undefined) {
          res.yesterday.ordernum = 0;
        }
        if (res.yesterday.turnover === undefined) {
          res.yesterday.turnover = 0;
        }
        vm.info.updatetime = res.updatetime;
        vm.info.yesterday = res.yesterday;
        vm.info.total = res.total;
        vm.info.third = res.third;
        vm.info.shop = res.shop;
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
  initEcharts: () => {
    let stime = 0;
    let etime = 0;
    if (vm.date.stime !== '') {
      stime = vm.date.stime;
    }
    if (vm.date.etime !== '') {
      etime = vm.date.etime;
    }
    cp.ajaxUrl({
      controller: 'shopinfo',
      cmd: 'com.rel.porg.pb.shop_home_page_img_req',
      data: {
        stime: Date.parse(new Date(stime)),
        etime: Date.parse(new Date(etime)),
        mode: 1,
      },
      success: res => {
        vm.date.stime = vm.formatTime(res.stime, 'en');
        vm.date.etime = vm.formatTime(res.etime, 'en');
        let echarts = require('./initecharts.js');
        let view = [];
        let visitor = [];
        let ordernum = [];
        let turnover = [];
        let date = [];
        for (let i in res.imgcounts) {
          if (res.imgcounts[i].view === undefined) {
            view.push(0);
          } else {
            view.push(res.imgcounts[i].view);
          }
          if (res.imgcounts[i].visitor === undefined) {
            visitor.push(0);
          } else {
            visitor.push(res.imgcounts[i].visitor);
          }
          if (res.imgcounts[i].ordernum === undefined) {
            ordernum.push(0);
          } else {
            ordernum.push(res.imgcounts[i].ordernum);
          }
          if (res.imgcounts[i].turnover === undefined) {
            turnover.push(0);
          } else {
            turnover.push(res.imgcounts[i].turnover);
          }
          date.push(time(res.imgcounts[i].time));
        }
        echarts.opt.series[0].data = view;
        echarts.opt.series[1].data = visitor;
        echarts.opt.series[2].data = ordernum;
        echarts.opt.series[3].data = turnover;
        echarts.opt.xAxis.data = date;
        echarts.getecharts();
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
});

require.ensure(['jquery-datetimepicker'], () => {
  require('jquery-datetimepicker');
  require('jquery-datetimepicker/jquery.datetimepicker.css');
  jQuery.datetimepicker.setLocale('ch');
  jQuery('#j-date-start').datetimepicker({
    minDate: new Date(),
    defaultTime: '9:00',
    onShow: function(ct) {
      this.setOptions({
        maxDate: jQuery('#j-date-end').val() ? jQuery('#j-date-end').val() : false,
      });
    },
    onChangeDateTime: function(ct, $i) {
      let start = $i.val();
      let end = jQuery('#j-date-end').val();
      if (end !== '' && (start > end || start === end)) {
        cp.layerMsg({
          msg: '开始时间必须小于结束时间',
        });
        jQuery('#j-date-start').val('');
      }
    },
  });
  jQuery('#j-date-end').datetimepicker({
    defaultTime: '9:00',
    onShow: function(ct) {
      this.setOptions({
        minDate: jQuery('#j-date-start').val() ? jQuery('#j-date-start').val() : false,
      });
    },
    onChangeDateTime: function(ct, $i) {
      let end = $i.val();
      let start = jQuery('#j-date-start').val();
      if (start !== '' && (start > end || start === end)) {
        cp.layerMsg({
          msg: '结束时间必须大于开始时间',
        });
        jQuery('#j-date-end').val('');
      }
    },
  });
}, 'ensure/datetimepicker');
function add_zero(m) {
  return m < 10 ? '0' + m : m;
};
function time(time_num) {
  let time = new Date(time_num);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  // let hour = time.getHours();
  // let minute = time.getMinutes();
  return year + '-' + add_zero(month) + '-' + add_zero(date);
}
moduleExport.content = require('./content.html');
vm.init();
moduleExport.vm = vm;
module.exports = moduleExport;
